package com.fuhaiwei.struts.action;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import com.fuhaiwei.basic.spring.BeanFactory;
import com.fuhaiwei.basic.utils.Logger;
import com.fuhaiwei.basic.utils.TextUtils;
import com.fuhaiwei.basic.utils.TextUtilsAware;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.TextProviderFactory;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.ValidationAwareSupport;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;

public abstract class AbstractValidationAction implements Action,
		ValidationAware, TextUtilsAware, TextProvider, LocaleProvider {
	
	private ValidationAwareSupport validationAware;
	private TextUtils textUtils;
	private TextProvider textProvider;
	private Locale locale;
	private Container container;

	public void addActionError(String anErrorMessage) {
		validationAware.addActionError(anErrorMessage);
	}

	public void addActionMessage(String aMessage) {
		validationAware.addActionMessage(aMessage);
	}

	public void addFieldError(String fieldName, String errorMessage) {
		int indexOf = errorMessage.indexOf("%fieldLabel%");
		if (indexOf != -1) {
			errorMessage = errorMessage.substring(0, indexOf)
					+ textUtils.label(fieldName)
					+ errorMessage.substring(indexOf + 12);
		}
		validationAware.addFieldError(fieldName, errorMessage);
	}

	public Collection<String> getActionErrors() {
		return validationAware.getActionErrors();
	}

	public Collection<String> getActionMessages() {
		return validationAware.getActionMessages();
	}

	public Container getContainer() {
		return container;
	}

	public Map<String, List<String>> getFieldErrors() {
		return validationAware.getFieldErrors();
	}

	public Locale getLocale() {
		if (locale != null) {
			return locale;
		}
		ActionContext ctx = ActionContext.getContext();
		if (ctx != null) {
			return ctx.getLocale();
		}
		Logger logger = (Logger) BeanFactory.getBean("logger");
		logger.info("Action context not initialized");
		return Locale.getDefault();
	}

	public String getText(String key) {
		return getTextProvider().getText(key);
	}

	public String getText(String key, List<Object> args) {
		return getTextProvider().getText(key, args);
	}

	public String getText(String key, String defaultValue) {
		return getTextProvider().getText(key, defaultValue);
	}

	public String getText(String key, String defaultValue, List<Object> args) {
		return getTextProvider().getText(key, defaultValue, args);
	}

	public String getText(String key, String defaultValue, List<Object> args,
			ValueStack stack) {
		return getTextProvider().getText(key, defaultValue, args, stack);
	}

	public String getText(String key, String defaultValue, String obj) {
		return getTextProvider().getText(key, defaultValue, obj);
	}

	public String getText(String key, String defaultValue, String[] args) {
		return getTextProvider().getText(key, defaultValue, args);
	}

	public String getText(String key, String defaultValue, String[] args,
			ValueStack stack) {
		return getTextProvider().getText(key, defaultValue, args, stack);
	}

	public String getText(String key, String[] args) {
		return getTextProvider().getText(key, args);
	}

	public TextProvider getTextProvider() {
		if (textProvider == null) {
			TextProviderFactory tpf = new TextProviderFactory();
			if (container != null) {
				container.inject(tpf);
			}
			textProvider = tpf.createInstance(getClass(), this);
		}
		return textProvider;
	}

	public ResourceBundle getTexts() {
		return getTextProvider().getTexts();
	}

	public ResourceBundle getTexts(String bundleName) {
		return getTextProvider().getTexts(bundleName);
	}

	public TextUtils getTextUtils() {
		return textUtils;
	}

	public ValidationAwareSupport getValidationAware() {
		return validationAware;
	}

	public boolean hasActionErrors() {
		return validationAware.hasActionErrors();
	}

	public boolean hasActionMessages() {
		return validationAware.hasActionMessages();
	}

	public boolean hasErrors() {
		return validationAware.hasErrors();
	}

	public boolean hasFieldErrors() {
		return validationAware.hasFieldErrors();
	}

	public boolean hasKey(String key) {
		return getTextProvider().hasKey(key);
	}

	public void setActionErrors(Collection<String> errorMessages) {
		validationAware.setActionErrors(errorMessages);
	}

	public void setActionMessages(Collection<String> messages) {
		validationAware.setActionMessages(messages);
	}

	@Inject
	public void setContainer(Container container) {
		this.container = container;
	}

	public void setFieldErrors(Map<String, List<String>> errorMap) {
		validationAware.setFieldErrors(errorMap);
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public void setTextProvider(TextProvider textProvider) {
		this.textProvider = textProvider;
	}

	public void setTextUtils(TextUtils textUtils) {
		this.textUtils = textUtils;
	}

	public void setValidationAware(ValidationAwareSupport validationAware) {
		this.validationAware = validationAware;
	}

}
